/*
 * addon_mem.h
 *
 *  Created on: 2010-1-7
 *      Author: gavinkwoe
 */

#ifndef ADDON_MEM_H_
#define ADDON_MEM_H_

#include "addon_features.h"
#include "addon_std.h"
#include "addon_objbase.h"

//#include "cx_typedef.h"
//#include "cx_memory.h"
//#include "cx_string.h"

//#include "ctype.h"

/*************************************************************************
* INTERFACE
*************************************************************************/

ADDON_INTERFACE_( IAddonAllocator, 1, IObject )
{
	__INHERIT_FROM( IObject )

	ADDON_METHOD( GetRemain )	( ADDON_REF(IObject) thiz, OUT unsigned long * psize );
	ADDON_METHOD( Alloc )		( ADDON_REF(IObject) thiz, IN unsigned long size, OUT void ** ppmem, ... );
	ADDON_METHOD( Free )		( ADDON_REF(IObject) thiz, IN void * pmem );
	ADDON_METHOD( Copy )		( ADDON_REF(IObject) thiz, IN void * dst, IN void * src, IN unsigned long size );
};

/*************************************************************************
* DECLARATION
*************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif	// #if defined(__cplusplus)

ADDON_IMPORT void				addon_heap_init( void );
ADDON_IMPORT void				addon_heap_deinit( void );
#if defined(__ADDON_DEBUG__)
ADDON_IMPORT void *				addon_heap_alloc( unsigned long size, unsigned char * file, unsigned long line );
#else	// #if defined(__ADDON_DEBUG__)
ADDON_IMPORT void *				addon_heap_alloc( unsigned long size );
#endif	// #if defined(__ADDON_DEBUG__)
ADDON_IMPORT void				addon_heap_free( void * addr );
ADDON_IMPORT void				addon_heap_dump( void );

ADDON_IMPORT ADDON_REF(IObject)	addon_create_allocator( void * reserved );

ADDON_IMPORT void				addon_memset( void * a, unsigned char b, unsigned long c );
ADDON_IMPORT void				addon_memcpy( void * a, const void * b, unsigned long c );

#if defined(__cplusplus)
}
#endif	// #if defined(__cplusplus)

/*************************************************************************
* MACRO
*************************************************************************/

#if defined(__ADDON_DEBUG__)
#define ADDON_HEAP_ALLOC( __size )	addon_heap_alloc( __size, (unsigned char *)__FILE__, (unsigned long)__LINE__ )
#else	// #if defined(__ADDON_DEBUG__)
#define ADDON_HEAP_ALLOC( __size )	addon_heap_alloc( __size )
#endif	// #if defined(__ADDON_DEBUG__)

#define ADDON_HEAP_FREE( __p )	\
		if ( NULL != (__p) ) \
		{ \
			addon_heap_free( (void *)__p ); \
			(__p) = NULL; \
		}

#define ADDON_MEMSET( a, b, c )				addon_memset( a, b, c )
#define ADDON_MEMCPY( a, b, c )				addon_memcpy( a, b, c ) // cx_memcpy( (QVOID *)a, (CONST QVOID *)b, (QUINT)c )
#define ADDON_MEMCMP( a, b, c )				memcpy( a, b, c ) // cx_memcpy( (QVOID *)a, (CONST QVOID *)b, (QUINT)c )

#define ADDON_WCSCMP( a, b )				wcscmp( a, b ) // cx_strcmp_w( (CONST QWCHAR *)a, (CONST QWCHAR *)b )		
#define ADDON_TOUPPER( c )					toupper( (int)c )

#define IAddonAllocator_VTBL( p )			ADDON_GET_VTBL_(p, IAddonAllocator)
#define IAddonAllocator_CALL( p, a )		IAddonAllocator_VTBL(p)->a
#define IAddonAllocator_GetRemain( p, a )	IAddonAllocator_CALL( p, GetRemain )( p, a )

#if defined(__ADDON_DEBUG__)
#define IAddonAllocator_Alloc( p, a, b )	IAddonAllocator_CALL( p, Alloc )( p, a, b, (unsigned char *)__FILE__, (unsigned long)__LINE__ )
#else	// #if defined(__ADDON_DEBUG__)
#define IAddonAllocator_Alloc( p, a, b )	IAddonAllocator_CALL( p, Alloc )( p, a, b )
#endif	// #if defined(__ADDON_DEBUG__)

#define IAddonAllocator_Free( p, a )		IAddonAllocator_CALL( p, Free )( p, a )
#define IAddonAllocator_Copy( p, a, b, c )	IAddonAllocator_CALL( p, Copy )( p, a, b, c )

#endif /* ADDON_MEM_H_ */
